<template>
  <!-- 产业技术规范书审核页面 -->
  <div class="specificationreview-page page-container">
    <div class="flex-auto overflow-y">
      <div class="full-height full-width no-scroll-x no-scroll-y flex-column">
        <data-list ref="dataList" :dataConfig="dataConfig" :tableData="tableData"></data-list>
        <upload-file ref="uploadFile" :uploadConfig="uploadConfig"></upload-file>
      </div>
    </div>
  </div>
</template>

<script>
import DataList from '@/components/business/page-comp/data-list/data-list.vue';
import UploadFile from '@/components/business/page-comp/upload-file/upload-file.vue';
import api from '../api/index.js';
export default {
  name: 'SpecificationreviewPage',
  components: { DataList, UploadFile },
  data() {
    return {
      tableData: {
        data: [],
        total: 0
      },
      dataConfig: {},
      uploadConfig: {},
    };
  },
  computed: {},
  beforeDestroy() { },
  created() {
    this.REVIEW_STATUS = [];
    for (let key in (this.$config.REVIEW_STATUS || {})) {
      this.REVIEW_STATUS.push({ ...this.$config.REVIEW_STATUS[key] });
    }
    this.dataConfig = {
      search: {
        labelWidth: '100px',
        loading: false,
        form: {
          planBatchCode: '',
          planBatchName: '',
          taskType: 2,
          status: 3,
        },
        conditions: [
          {
            label: '批次编号',
            prop: 'planBatchCode',
            type: 'input',
          },
          {
            label: '批次名称',
            prop: 'planBatchName',
            type: 'input',
          },
        ],
        searchCB: this.search,
      },
      table: {
        config: {
          select: false,
          selectCallback: () => { }
        },
        columns: [
          {
            label: '批次编号',
            prop: 'planBatchCode',
            width: '140px',
            type: 'text',
          },
          {
            label: '批次名称',
            prop: 'planBatchName',
            type: 'text'
          },
          {
            label: '文件名',
            prop: 'fileName',
            type: 'text'
          },
          {
            label: '是否关联技术规范书',
            prop: 'techFileName',
            width: '160px',
            type: 'text',
            callback: (item) => { return item.techFileName ? '已关联' : '未关联' },
          },
          {
            label: '技术规范书审核状态',
            prop: 'techStatus',
            width: '160px',
            type: 'text',
            callback: (item) => { return item.techStatus ? this.getStatusText(item.techStatus) : '未关联' },
          },
          {
            label: '操作',
            prop: 'status',
            type: 'operates',
            fixed: 'right',
            width: '300',
            operates: [
              {
                label: '查看审核结果',
                type: 'link',
                callback: this.showDetail,
                show: (item) => {
                  // 未关联不展示查看审核结果按钮
                  return !!item.techStatus;
                }
              },
              {
                label: '技术审核结果下载',
                type: 'link',
                callback: this.downloadResult,
                show: (item) => {
                  // 技术审核结果下载按钮只有当状态为“审核完成”时才展示
                  return String(item.techStatus) === this.$config.REVIEW_STATUS.REVIEWED.value;
                }
              },
              {
                label: '上传技术规范书并审核',
                type: 'link',
                callback: this.uploadFile,
                show: (item) => {
                  // 上传技术规范书并审核按钮只有当状态为“待审核”时才展示
                  // return String(item.techStatus) === this.$config.REVIEW_STATUS.BEFOREREVIEW.value || !item.techStatus;
                  // 上传技术规范书并审核按钮只有当“未关联技术规范书”状态时才展示
                  return !item.techFileName;
                }
              },
              {
                label: '强制结束',
                type: 'link',
                callback: this.forceEnd,
                show: (item) => {
                  // 强制结束按钮只有当状态为“审核中”时才展示
                  return String(item.techStatus) === this.$config.REVIEW_STATUS.REVIEWING.value;
                }
              },
            ]
          },
        ],
      }
    };
    // 配置文件上传组件参数
    this.uploadConfig = {
      title: '上传技术规范书',
      labelWidth: '120px',
      inputWidth: '260px',
      action: '/plan/createIndustryTask',
      multiple: false,
      limit: 1,
      accept: '.zip',
      uploadTips: '只能上传.zip压缩包文件，且不超过500MB',
      form: {
        planBatchCode: '',
        // 任务类型
        // 2：商务
        // 3：技术(技术规范书上传)
        businessType: '3',
        planId: '',
      },
      conditions: [
        {
          label: '批次编号',
          prop: 'planBatchCode',
          type: 'input',
          disabled: true,
        },
        {
          label: '技术规范书',
          prop: 'fileStream',
          type: 'file',
        },
      ],
      formRules: {
        planBatchCode: [
          { required: true, message: '请输入批次编号', trigger: 'blur' },
        ],
      },
    };
  },
  mounted() {
    this.$refs['dataList'].search();
  },
  methods: {
    getStatusText(status) {
      return (this.REVIEW_STATUS.find(item => {
        return String(item.value) === String(status);
      }) || {}).name;
    },
    showDetail(item) {
      this.$router.push({ path: `/review/subtask/specificationdetail`, query: { id: item.id } });
    },
    downloadResult(item) {
      const filePath = item.techResultFilepath;
      if (!filePath || !filePath.length) {
        this.$message.error(`技术规范书审核结果下载失败，文件内容为空`);
        return;
      }
      const fileName = filePath.split('/')[filePath.split('/').length - 1];
      this.$log.debug(`技术规范书审核结果文件为：`, fileName);
      api.downResultExcelFile({
        filePath
      })
        .then((res) => {
          this.$log.debug(`技术规范书审核结果下载，文件大小为：`, res.data.size);

          if (!res.data.size) {
            this.$message.error(`技术规范书审核结果下载失败，文件内容为空`);
            return;
          }
          this.$utils.downloadBlobFile(fileName, res.data);
          this.$message.success(`技术规范书审核结果下载成功`);
        })
        .catch((e) => {
          this.$log.error(`技术规范书审核结果下载失败，错误信息为：`, e);
        });
    },
    forceEnd(item) {
      this.$confirm(`确认强制结束该审核任务?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.forceFailTechReviewMainTask({
            id: item.id
          })
            .then((res) => {
              if (res.success) {
                this.$log.debug(`审核任务强制结束成功`);
                this.$message.success(`审核任务强制结束成功`);
                this.$refs['dataList'].search();
              } else {
                this.$log.error(`审核任务强制结束失败，错误信息为：`, res.message);
                this.$message.error(`审核任务强制结束失败：` + (res.message || ''));
              }
            })
            .catch((e) => {
              this.$log.error(`审核任务强制结束失败，错误信息为：`, e);
            });
        }).catch((e) => {
          this.$log.debug(`取消审核任务强制结束操作`, e);
        });
    },
    uploadFile(item) {
      this.uploadConfig.form.planBatchCode = item.planBatchCode;
      this.uploadConfig.form.planId = item.id;
      this.$refs['uploadFile'].open();
    },
    search(pageInfo, searchForm) {
      this.dataConfig.search.loading = true;
      let params = {
        ...pageInfo,
        ...searchForm,
      };
      this.$log.debug('产业技术规范书审核列表查询，查询参数为：', params);
      api.queryTechMainTasks(params)
        .then((res) => {
          this.dataConfig.search.loading = false;
          if (res.success) {
            this.$log.debug('查询产业计划审核任务列表成功');
            this.tableData = { data: res.data.mainTaskList, total: res.data.total };
          } else {
            this.$log.error('查询产业计划审核任务列表失败，错误信息为：', res.message);
            this.$message.error('查询产业计划审核任务列表失败：' + (res.message || ''));
          }
        })
        .catch((e) => {
          this.dataConfig.search.loading = false;
          this.$log.error('查询产业计划审核任务列表失败，错误信息为：', e);
        });
    },
  }
};
</script>

<style lang="scss" scoped>
.specificationreview-page {}
</style>
